//package com.example.myproject.web;
//
//
//import com.example.myproject.model.MessageModel;
//import com.example.myproject.other.HelloEventHandler;
//import com.example.myproject.other.HelloEventHandler2;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RestController;
//import reactor.jarjar.com.lmax.disruptor.RingBuffer;
//import reactor.jarjar.com.lmax.disruptor.dsl.Disruptor;
//
//import java.util.concurrent.Executor;
//import java.util.concurrent.Executors;
//
//@RestController
//@RequestMapping("/disruptor")
//@Api(tags = "MessagePushController", description = "2322")
//public class MessagePushController {
//    private Logger log = LoggerFactory.getLogger(MessagePushController.class);
//
//    /**
//     * 队列
//     */
//    private volatile Disruptor<MessageModel> disruptor;
//
//    @ApiOperation(value="push", notes="push")
//    @RequestMapping(value="/push", method= RequestMethod.GET)
//    public void push( String message) {
//        RingBuffer<MessageModel> messageModelRingBuffer = getDisruptor().getRingBuffer();
//        log.info("record the message: {}",message);
//        //获取下一个Event槽的下标
//        long sequence = messageModelRingBuffer.next();
//        try {
//            //给Event填充数据
//            MessageModel event = messageModelRingBuffer.get(sequence);
//            event.setMessage(message);
//            log.info("往消息队列中添加消息：{}", event);
//        } catch (Exception e) {
//            log.error("failed to add event to messageModelRingBuffer for : e = {},{}",e,e.getMessage());
//        } finally {
//            //发布Event，激活观察者去消费，将sequence传递给改消费者
//            //注意最后的publish方法必须放在finally中以确保必须得到调用；如果某个请求的sequence未被提交将会堵塞后续的发布操作或者其他的producer
//            messageModelRingBuffer.publish(sequence);
//        }
//    }
//
//    private Disruptor<MessageModel> getDisruptor() {
//        if (disruptor != null) {
//            return disruptor;
//        }
//        synchronized (MessagePushController.class) {
//            if (disruptor != null) {
//                return disruptor;
//            }
//            // 启动消息队列
//            Executor executor = Executors.newCachedThreadPool();
//            int bufferSize = 1024;
//            disruptor = new Disruptor<>(MessageModel::new, bufferSize, executor);
//            //disruptor.handleEventsWith(MessageHandlerHolder.values().toArray(new MessageHandler[0]));
//
//            //设置事件业务处理器---消费者
//            disruptor.handleEventsWith(new HelloEventHandler(),new HelloEventHandler2());
//            disruptor.start();
//            return disruptor;
//        }
//    }
//
//}
